package com.top.ruizhong.main.video

import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.google.android.material.tabs.TabLayout
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.top.ruizhong.R
import com.top.ruizhong.mode.vm.VideoViewModel
import com.top.ruizhong.popup.VideoDiscussPopup
import kotlinx.android.synthetic.main.fragment_main_video.*


class MainVideoFragment : ViewModeFragment<VideoViewModel>() {

    private val playerEXOAdapter: PlayerEXOAdapter = PlayerEXOAdapter()

    private var currentPosition: Int = 0

    companion object {
        fun newInstance(): MainVideoFragment {
            return MainVideoFragment()
        }
    }


    private val videoDiscussPopup: VideoDiscussPopup by lazy {
        VideoDiscussPopup(context = requireContext(), onConfirm = {

        })
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_video

    override fun viewModel(): VideoViewModel {
        return createViewModel()
    }

    override fun initView() {

        BarUtils.addMarginTopEqualStatusBarHeight(tabVideoKind)

        tabVideoKind.addTab(tabVideoKind.newTab().setText("朋友"))
        tabVideoKind.addTab(tabVideoKind.newTab().setText("推荐"))
        tabVideoKind.addTab(tabVideoKind.newTab().setText("同城"))

        tabVideoKind.apply {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(tab: TabLayout.Tab?) = Unit

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                }

                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val text: TextView = TextView(context).apply {
                        setTextColor(ColorUtils.getColor(R.color.colorMain))
                        textSize = 16f
                        gravity = Gravity.CENTER
                        paint.isFakeBoldText = true
                    }
                    text.text = tab?.text
                    tab?.customView = text
                }
            })
        }

        vp2VideoBody.apply {
            orientation = ViewPager2.ORIENTATION_VERTICAL
            val child: View = getChildAt(0)
            if (child is RecyclerView) {
                child.setOverScrollMode(View.OVER_SCROLL_NEVER)
                child.setItemViewCacheSize(0)
            }
        }.adapter = playerEXOAdapter

        vp2VideoBody.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)

                if (currentPosition > position) {
                    val bottomPausePosition: Int = position + 1
                    val bottomReleasePosition: Int = position + 2
                    val size: Int = playerEXOAdapter.data.size
                    if (bottomPausePosition < size) {
                        playerEXOAdapter.pause(bottomPausePosition)
                    }
                    if (bottomReleasePosition < size) {
                        playerEXOAdapter.release(bottomReleasePosition)
                    }
                } else if (currentPosition < position) {
                    val topPausePosition: Int = position - 1
                    val topReleasePosition: Int = position - 2
                    if (topPausePosition >= 0) {
                        playerEXOAdapter.pause(topPausePosition)
                    }
                    if (topReleasePosition >= 0) {
                        playerEXOAdapter.release(topReleasePosition)
                    }
                }
                playerEXOAdapter.play(position)
                currentPosition = position
            }
        })

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.indexAudioList()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.indexAudioList(isLoadMore = true)
        }


        playerEXOAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tvControlsDiscuss) {
                videoDiscussPopup.setFillData().showPopupWindow()
            }
        }
    }

    override fun initData() {
        mSmartRefreshLayout.autoRefresh()
    }

    override fun initViewMode() {
        viewModel.audioListLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                mSmartRefreshLayout.finishRefresh(false)
                mSmartRefreshLayout.finishLoadMore(false)
                toast(response.throwable?.msg ?: "未知错误")
            }
            if (response.status == Status.SUCCESS) {
                mSmartRefreshLayout.finishRefresh()
                mSmartRefreshLayout.finishLoadMore()
                response.httpData?.let {
                    if (it.loadMore) {
                        playerEXOAdapter.addData(it.data)
                    } else {
                        playerEXOAdapter.releaseAll()
                        playerEXOAdapter.setList(it.data)
                    }
                }
            }
        })
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onPause() {
        super.onPause()
        playerEXOAdapter.pause(currentPosition)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        playerEXOAdapter.releaseAll()
    }
}